<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" label-width="68px">
      <el-form-item label="姓名" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入姓名"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      
      <el-form-item label="来源" prop="source">
        <el-select v-model="queryParams.source" placeholder="请选择来源" clearable size="small">
          <el-option label="售前" value="pre" />
          <el-option label="售后" value="post" />
        </el-select>
      </el-form-item>
      
      <el-form-item label="导入时间" prop="importTime">
        <el-date-picker
          v-model="queryParams.importTime"
          size="small"
          style="width: 240px"
          value-format="yyyy-MM-dd"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>
      
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">查询</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
    
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-user"
          size="mini"
          :disabled="multiple"
          @click="handleAssign"
        >分配</el-button>
      </el-col>
    </el-row>
    
    <el-table v-loading="loading" :data="leadsList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="姓名" align="center" prop="name" />
      <el-table-column label="手机号" align="center" prop="phone" />
      <el-table-column label="地址" align="center" prop="address" />
      <el-table-column label="导入时间" align="center" prop="importTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.importTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="来源" align="center" prop="source">
        <template slot-scope="scope">
          <el-tag :type="scope.row.source === 'pre' ? 'primary' : 'success'">
            {{ scope.row.source === 'pre' ? '售前' : '售后' }}
          </el-tag>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
    
    <!-- 分配弹窗 -->
    <el-dialog title="分配线索" :visible.sync="assignFormVisible" width="500px" append-to-body>
      <el-form ref="assignForm" :model="assignForm" label-width="80px">
        <el-form-item label="分配给" prop="assignTo">
          <el-select 
            v-model="assignForm.assignTo" 
            filterable 
            placeholder="请选择客服"
            style="width: 100%">
            <el-option
              v-for="item in customerServiceOptions"
              :key="item.id"
              :label="item.name"
              :value="item.id">
            </el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitAssign">确 定</el-button>
        <el-button @click="cancelAssign">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { parseTime } from "@/utils/ruoyi";

export default {
  name: "LeadsRecycle",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 总条数
      total: 0,
      // 回收池线索表格数据
      leadsList: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        name: undefined,
        source: undefined,
        importTime: []
      },
      // 分配表单可见性
      assignFormVisible: false,
      // 分配表单数据
      assignForm: {
        assignTo: undefined
      },
      // 客服选项
      customerServiceOptions: []
    };
  },
  created() {
    this.getList();
    this.getCustomerServiceOptions();
  },
  methods: {
    /** 查询回收池线索列表 */
    getList() {
      this.loading = true;
      // 使用mock数据
      this.leadsList = this.getMockData();
      this.total = this.leadsList.length;
      this.loading = false;
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 多选框选中数据 */
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!=1
      this.multiple = !selection.length
    },
    /** 分配按钮操作 */
    handleAssign() {
      if (this.ids.length === 0) {
        this.$message.warning("请先选择要分配的线索");
        return;
      }
      this.assignForm.assignTo = undefined;
      this.assignFormVisible = true;
    },
    /** 提交分配 */
    submitAssign() {
      this.$refs.assignForm.validate(valid => {
        if (valid) {
          // 这里应该调用API提交分配，目前使用mock
          const customerService = this.customerServiceOptions.find(p => p.id === this.assignForm.assignTo);
          this.$message.success(`已成功将 ${this.ids.length} 条线索分配给 ${customerService.name}`);
          this.assignFormVisible = false;
          this.getList(); // 重新加载数据
        }
      });
    },
    /** 取消分配 */
    cancelAssign() {
      this.assignFormVisible = false;
      this.$refs.assignForm.resetFields();
    },
    /** 获取客服选项 */
    getCustomerServiceOptions() {
      // 使用mock数据
      this.customerServiceOptions = this.getMockCustomerServices();
    },
    /** 时间格式化 */
    parseTime(time, format) {
      return parseTime(time, format);
    },
    /** 获取mock数据 */
    getMockData() {
      return [
        {
          id: 1,
          name: "张三",
          phone: "13800138000",
          address: "北京市朝阳区",
          importTime: "2025-10-20",
          source: "pre"
        },
        {
          id: 2,
          name: "李四",
          phone: "13900139000",
          address: "上海市浦东新区",
          importTime: "2025-10-21",
          source: "post"
        },
        {
          id: 3,
          name: "王五",
          phone: "13700137000",
          address: "广州市天河区",
          importTime: "2025-10-22",
          source: "pre"
        },
        {
          id: 4,
          name: "赵六",
          phone: "13600136000",
          address: "深圳市南山区",
          importTime: "2025-10-23",
          source: "post"
        },
        {
          id: 5,
          name: "钱七",
          phone: "13500135000",
          address: "杭州市西湖区",
          importTime: "2025-10-24",
          source: "pre"
        }
      ];
    },
    /** 获取mock客服数据 */
    getMockCustomerServices() {
      return [
        { id: 1, name: "客服A" },
        { id: 2, name: "客服B" },
        { id: 3, name: "客服C" },
        { id: 4, name: "客服D" }
      ];
    }
  }
};
</script>